/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.category;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategoryColor;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategoryProperty;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategorySpec;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryColorService;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategorySpecService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategory;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryService;

/**
 * 商品分类Controller
 *
 * @author 罗天文
 * @version 2018-05-15
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/category/hkShopCategory")
public class HkShopCategoryController extends BaseController {

    @Autowired
    private HkShopCategoryService hkShopCategoryService;

    @ModelAttribute
    public HkShopCategory get(@RequestParam(required = false) String id) {
        HkShopCategory entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkShopCategoryService.get(id);
        }
        if (entity == null) {
            entity = new HkShopCategory();
        }
        return entity;
    }

    @RequiresPermissions("shop:category:hkShopCategory:view")
    @RequestMapping(value = {"list", ""})
    public String list(HkShopCategory hkShopCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<HkShopCategory> list = hkShopCategoryService.findList(hkShopCategory);
        model.addAttribute("list", list);
        return "hk/shop/category/hkShopCategoryList";
    }

    @RequiresPermissions("shop:category:hkShopCategory:view")
    @RequestMapping(value = "form")
    public String form(HkShopCategory hkShopCategory, Model model) {
        if (hkShopCategory.getParent() != null && StringUtils.isNotBlank(hkShopCategory.getParent().getId())) {
            hkShopCategory.setParent(hkShopCategoryService.get(hkShopCategory.getParent().getId()));
            // 获取排序号，最末节点排序号+30
            if (StringUtils.isBlank(hkShopCategory.getId())) {
                HkShopCategory hkShopCategoryChild = new HkShopCategory();
                hkShopCategoryChild.setParent(new HkShopCategory(hkShopCategory.getParent().getId()));
                List<HkShopCategory> list = hkShopCategoryService.findList(hkShopCategory);
                if (list.size() > 0) {
                    hkShopCategory.setSort(list.get(list.size() - 1).getSort());
                    if (hkShopCategory.getSort() != null) {
                        hkShopCategory.setSort(hkShopCategory.getSort() + 30);
                    }
                }
            }
        }
        if (hkShopCategory.getSort() == null) {
            hkShopCategory.setSort(30);
        }
        model.addAttribute("hkShopCategory", hkShopCategory);
        return "hk/shop/category/hkShopCategoryForm";
    }

    @RequiresPermissions("shop:category:hkShopCategory:edit")
    @RequestMapping(value = "save")
    public String save(HkShopCategory hkShopCategory, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, hkShopCategory)) {
            return form(hkShopCategory, model);
        }
        hkShopCategoryService.save(hkShopCategory);
        addMessage(redirectAttributes, "保存商品分类成功");
        return "redirect:" + Global.getAdminPath() + "/shop/category/hkShopCategory/?repage";
    }

    @RequiresPermissions("shop:category:hkShopCategory:edit")
    @RequestMapping(value = "delete")
    public String delete(HkShopCategory hkShopCategory, RedirectAttributes redirectAttributes) {
        hkShopCategoryService.delete(hkShopCategory);
        addMessage(redirectAttributes, "删除商品分类成功");
        return "redirect:" + Global.getAdminPath() + "/shop/category/hkShopCategory/?repage";
    }

    @RequiresPermissions("shop:category:hkShopCategory:view")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<HkShopCategory> list = hkShopCategoryService.findList(new HkShopCategory());
        for (int i = 0; i < list.size(); i++) {
            HkShopCategory e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1)) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }


    @RequiresPermissions("shop:category:hkShopCategory:view")
    @ResponseBody
    @RequestMapping(value = "merchTreeData")
    public List<Map<String, Object>> merchTreeData(@RequestParam(required = false) String extId, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<HkShopCategory> list = hkShopCategoryService.findMerchList(new HkShopCategory());
        for (int i = 0; i < list.size(); i++) {
            HkShopCategory e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1)) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }


    @RequiresPermissions("shop:category:hkShopCategory:view")
    @RequestMapping(value = "getJson")
    @ResponseBody
    public Map getJson(HkShopCategoryProperty hkShopCategoryProperty, HttpServletRequest request, HttpServletResponse response, Model model) {
        Map map = Maps.newHashMap();
        HkShopCategorySpec hkShopCategorySpec = new HkShopCategorySpec();
        hkShopCategorySpec.setCategory(hkShopCategoryProperty.getCategory());
        map.put("specs", hkShopCategorySpecService.findList(hkShopCategorySpec));
        HkShopCategoryColor hkShopCategoryColor = new HkShopCategoryColor();
        hkShopCategoryColor.setCategory(hkShopCategoryProperty.getCategory());
        map.put("colors", hkShopCategoryColorService.findList(hkShopCategoryColor));
        return map;
    }

    @Autowired
    private HkShopCategorySpecService hkShopCategorySpecService;
    @Autowired
    private HkShopCategoryColorService hkShopCategoryColorService;
}